package team.fivez.defaultmanage.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import team.fivez.defaultmanage.dao.DefaultApplicationDao;
import team.fivez.defaultmanage.dao.IndustryInfoDao;
import team.fivez.defaultmanage.dao.RedoApplicationDao;
import team.fivez.defaultmanage.domain.Classification;
import team.fivez.defaultmanage.domain.IndustryInfo;
import team.fivez.defaultmanage.mediumDomain.FullClassification;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Component
public class IndustryInfoService {


    private final DefaultApplicationDao defaultApplicationDao;

    private final RedoApplicationDao redoApplicationDao;

    private final IndustryInfoDao industryInfoDao;

    @Autowired
    public IndustryInfoService(DefaultApplicationDao defaultApplicationDao, RedoApplicationDao redoApplicationDao, IndustryInfoDao industryInfoDao) {
        this.defaultApplicationDao = defaultApplicationDao;
        this.redoApplicationDao = redoApplicationDao;
        this.industryInfoDao = industryInfoDao;
    }

    //获取行业违约情况
    public List<FullClassification> getIndustryDefaultSituation(){
        List<FullClassification> fullList = new ArrayList<>();
        industryInfoDao.deleteAllDefault();
        for (IndustryInfo industryInfo : defaultApplicationDao.getDefaultIndustryInfo()) {
            industryInfoDao.insertInfoToDefault(industryInfo.getIndustry(),
                    String.valueOf(industryInfo.getApprovementTime().toLocalDateTime().getYear()));
        }

        List<String> yearList = industryInfoDao.getYearListFromDefault();
        List<String> industryList = industryInfoDao.getIndustryListFromDefault();
        yearList.sort(Comparator.naturalOrder());

        for (String year : yearList) {
            List<Classification> classifyList = new ArrayList<>();
            for (String industry : industryList) {
                Integer integer = industryInfoDao.getCountByIndustryAndYearFromDefault(industry, year);
                if (integer!=null){
                    classifyList.add(new Classification(industry,integer));
                }
            }
            fullList.add(new FullClassification(year,classifyList));
        }

        return fullList;
    }


    //获取行业重生情况
    public List<FullClassification> getIndustryRedoSituation(){
        List<FullClassification> fullList = new ArrayList<>();
        industryInfoDao.deleteAllRedo();
        for (IndustryInfo industryInfo : redoApplicationDao.getRedoIndustryInfo()) {
            industryInfoDao.insertInfoToRedo(industryInfo.getIndustry(),
                    String.valueOf(industryInfo.getApprovementTime().toLocalDateTime().getYear()));
        }

        List<String> yearList = industryInfoDao.getYearListFromRedo();
        List<String> industryList = industryInfoDao.getIndustryListFromRedo();
        yearList.sort(Comparator.naturalOrder());

        for (String year : yearList) {
            List<Classification> classifyList = new ArrayList<>();
            for (String industry : industryList) {
                Integer integer = industryInfoDao.getCountByIndustryAndYearFromRedo(industry, year);
                if (integer!=null){
                    classifyList.add(new Classification(industry,integer));
                }
            }


            fullList.add(new FullClassification(year,classifyList));
        }

        return fullList;


    }



}
